package org.ruoyi.chat.service.chat.impl;

import okhttp3.*;

import java.io.IOException;

/**
 * @author jianxiongliao
 * @date 2025/7/19 18:13
 */
public class DashScopeCompatibleExample1 {
    private static final String API_KEY = "sk-7dae9a7bcb4c4c239fdd8a55b7ec974c";
    private static final String API_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";

    public static void main(String[] args) {
        // 增加超时时间
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(30, java.util.concurrent.TimeUnit.SECONDS)
                .readTimeout(30, java.util.concurrent.TimeUnit.SECONDS)
                .writeTimeout(30, java.util.concurrent.TimeUnit.SECONDS)
                .build();

        String requestBody = "{" +
                "\"model\": \"deepseek-r1\"," +
                "\"messages\": [" +
                "{" +
                "\"role\": \"user\"," +
                "\"content\": \"你是谁啊\"" +
                "}" +
                "]" +
                "}";

        RequestBody body = RequestBody.create(MediaType.parse("application/json"), requestBody);

        Request request = new Request.Builder()
                .url(API_URL)
                .addHeader("Authorization", "Bearer " + API_KEY)
                .addHeader("Content-Type", "application/json")
                .post(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                System.out.println(response.body().string());
            } else {
                System.out.println("请求失败，状态码: " + response.code());
                System.out.println("响应信息: " + response.message());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
